package client;


import iFace.WaveServerInterface;

import java.awt.*;
import javax.swing.*;



import java.awt.image.*;
import java.util.*;
import java.rmi.Naming;
import java.rmi.RMISecurityManager;

public class WaveClient extends JFrame {

	private static final long serialVersionUID = 1L;
    final int IMG_SIZE=512;
    byte[][] pic = new byte[IMG_SIZE][IMG_SIZE];
    int[] lut = new int[256];

    BufferedImage img = new BufferedImage(IMG_SIZE, IMG_SIZE,BufferedImage.TYPE_INT_BGR);

    public void start(String[] args) {
        setTitle("Wavelet");
        setSize(IMG_SIZE,IMG_SIZE+30);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);

        // criar um mapa de cores (tons de cinzento)
        for (int i=0; i<256; i++) lut[i]=2*i+256*i*2+256*256*i*2;

        Image im = getImage("im1.jpg");
        int[] pixels = new int[IMG_SIZE * IMG_SIZE];

        // converts Image to array of byte
        PixelGrabber pg = new PixelGrabber(im, 0, 0, IMG_SIZE, IMG_SIZE, pixels, 0, IMG_SIZE);
        try {
            pg.grabPixels();
        } catch (InterruptedException ee) {
            System.err.println("interrupted waiting for pixels!");
             return;
        }
        for(int i=0; i<IMG_SIZE; i++)
           for(int j=0; j<IMG_SIZE; j++)
                pic[i][j] = (byte) (((pixels[j*IMG_SIZE + i])&0xff)/2);
           	//if ((pixels[j*IMG_SIZE + i]&0x80)==0x80) pic[i][j]=127;
            //else pic[i][j]=0;

		repaint();

		System.setSecurityManager(new RMISecurityManager());

		if (args.length != 1) {
			System.err.println("Usage: client.WaveClient <server-rmi-url>");
			System.exit(-1);
		}

		String fullname = args[0];
		WaveServerInterface server = null;
		try {
			server = (WaveServerInterface) Naming.lookup(fullname);
		} catch (Exception e) {
			System.out.println("Caught an exception doing name lookup on "
					+ fullname + ": " + e);
			System.exit(-1);
		}

		try {
			double start = new Date().getTime();
			this.pic = server.getWavelet(pic, IMG_SIZE);
			System.out.println("Demorou " + (new Date().getTime() - start)
					+ " ms");
			repaint();
		} catch (Exception e) {
			System.out.println("Exception: " + e);
			System.exit(-1);
		}

    }

    public void paint(Graphics g) {

        for(int i=0; i<IMG_SIZE; i++)
           for(int j=0; j<IMG_SIZE; j++) {
               //System.out.print(pic[i][j] + " ");
               img.setRGB(i,j,lut[pic[i][j]]);
            }
       Graphics2D g2 = (Graphics2D) g;
       g2.drawImage(img,0,25,this);
    }

    protected Image getImage(String fileName) {
        Image img = getToolkit().getImage(fileName);
        try {
           MediaTracker tracker = new MediaTracker(this);
           tracker.addImage(img, 0);
           tracker.waitForID(0);
           tracker.removeImage(img,0);
        } catch (Exception e) { e.printStackTrace(); }
        return img;
    }

    public static void main(String[] arg) {
        WaveClient mc = new WaveClient();
        mc.start(arg);
    }
}

